import ITool from '../ITool';

/**
 * 书签工具箱
 * @implements {ITool}
 */
class BookMarkTool extends ITool {
  constructor(_top, _impl) {
    super(_top, _impl);
    this.name = 'BookMarkTool';
    this.desc = '书签工具箱';
    this.bookmarkList = [];
  }

  onAdd() {
    super.onAdd();
  }

  onRemove() {
    super.onRemove();
  }

  /**
   * 设置书签列表
   * @param {Array} list
   */
  setBookmarkList(list) {
    this.bookmarkList = list;
  }

  /**
   * 添加书签
   * @param {Object} bookmark
   */
  async add(bookmark) {
    let found = this.bookmarkList.find((item) => item.name === bookmark.name);
    if (found) {
      return {
        message: '存在同名书签',
        success: false
      };
    }
    // 调用添加逻辑
    await this.doAdd(bookmark);
    this.bookmarkList.push(bookmark);
    return {
      message: '添加成功',
      success: true
    };

  }

  /**
   * 删除书签
   * @param {Object} item
   */
  async delete(bookmark) {
    let found = this.bookmarkList.find((item) => item.name === bookmark.name);
    if (found) {
      // 调用删除逻辑
      await this.doDelete(bookmark);
      // 移出书签列表
      this.bookmarkList.splice(this.bookmarkList.indexOf(bookmark), 1);
      return {
        message: '删除成功',
        success: true
      };
    }
    return {
      message: '删除失败，没找到书签',
      success: true
    };

  }

  /**
   * 书签添加实现
   * @param {Object} item
   * @ignore
   */
  doAdd() {
    return new Promise((resolve) => {
      // TODO add bookmark
      resolve(true);
    });
  }

  /**
   * 书签删除实现
   * @param {Object} item
   * @ignore
   */
  doDelete() {
    return new Promise((resolve) => {
      // TODO delete bookmark
      resolve(true);
    });
  }
}

export default BookMarkTool;
